package com.xqboss.apps.service.consumer;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.consumer.ConsumerFriends;
import com.xqboss.apps.domain.consumer.ConsumerFriendsApply;
import com.xqboss.apps.domain.consumer.ConsumerInteractMsg;
import com.xqboss.apps.dto.consumer.ConsumerFriendsApplyDto;
import com.xqboss.apps.enums.consumer.ConsumerApplyStatusEnum;
import com.xqboss.apps.enums.consumer.InteractMsgEnum;
import com.xqboss.apps.mapper.consumer.ConsumerFriendsApplyMapper;
import com.xqboss.apps.vo.consumer.ConsumerFriendsApplyVo;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.core.domain.entity.SysUser;
import com.xqboss.common.utils.DateUtils;
import com.xqboss.common.utils.LocalDateUtils;
import com.xqboss.common.utils.SecurityUtils;
import com.xqboss.common.utils.SocketUtil;
import com.xqboss.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
@Service
public class ConsumerFriendsApplyService extends ServiceImpl<ConsumerFriendsApplyMapper, ConsumerFriendsApply> {

    @Resource
    private ConsumerFriendsApplyMapper consumerFriendsApplyMapper;
    @Resource
    private ConsumerFriendsService consumerFriendsService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private ConsumerInteractMsgService consumerInteractMsgService;

    public List<ConsumerFriendsApplyVo> friendsApplyList(ConsumerFriendsApplyDto dto) {
        List<ConsumerFriendsApplyVo> result = null;
        if (ObjUtil.equal(dto.getApplyType(), 2)) {
            dto.setInitUserId(SecurityUtils.getUserId());
            result = consumerFriendsApplyMapper.selectMeApplyList(dto);
        } else {
            dto.setReceiveUserId(SecurityUtils.getUserId());
            result = consumerFriendsApplyMapper.selectFriendApplyList(dto);
        }
        return result;
    }

    public R edit(ConsumerFriendsApplyDto dto) {
        if (ObjUtil.isNull(dto.getId()) || ObjUtil.isNull(dto.getReceiveUserId())) {
            return R.fail("参数错误");
        }
        ConsumerFriendsApply friendsApply = consumerFriendsApplyMapper.selectById(dto.getId());
        Objects.requireNonNull(friendsApply, "资源不存在");

        ConsumerFriendsApply apply = new ConsumerFriendsApply();
        apply.setId(friendsApply.getId());
        apply.setStatus(dto.getStatus().getValue());
        consumerFriendsApplyMapper.updateById(apply);
        //检测是否已经成为好友
        LambdaQueryWrapper<ConsumerFriends> friendWp = new LambdaQueryWrapper<>();
        friendWp.eq(ConsumerFriends::getUserId, dto.getInitUserId());
        friendWp.eq(ConsumerFriends::getFriendUserId, dto.getReceiveUserId());
        ConsumerFriends one = consumerFriendsService.getOne(friendWp);
        if (ObjUtil.isNotNull(one)) {
            return R.fail("已添加为好友");
        }

        if (ObjUtil.equal(dto.getStatus().getValue(), ConsumerApplyStatusEnum.AGREED.getValue())) {
            ConsumerFriends friends = new ConsumerFriends();
            friends.setUserId(dto.getInitUserId());
            friends.setFriendUserId(dto.getReceiveUserId());
//            friends.setReamrks(dto.getNickName());
            friends.setAddTime(DateUtils.getNowDate());
//            friends.setAddUserId();
            consumerFriendsService.save(friends);
            friends.setId(null);
            friends.setUserId(dto.getReceiveUserId());
            friends.setFriendUserId(dto.getInitUserId());
//            friends.setReamrks(SecurityUtils.getNickName());
            consumerFriendsService.save(friends);
        }

        //站内信V23.9.7 编辑好友申请记录
        ConsumerInteractMsg msg = new ConsumerInteractMsg();
        msg.setUserId(dto.getReceiveUserId());
        if (dto.getStatus().getValue() == 2) {
            msg.setContent(SecurityUtils.getNickName() + InteractMsgEnum.FRIEND_CONSENT.getMsg());
            msg.setType(InteractMsgEnum.FRIEND_CONSENT);
            //同意添加好友
            SocketUtil.sendOneMessage(String.valueOf(dto.getReceiveUserId()), SecurityUtils.getNickName() + InteractMsgEnum.FRIEND_CONSENT.getMsg(),
                    InteractMsgEnum.FRIEND_CONSENT.getValue(), Long.valueOf(friendsApply.getId()));
            //提醒自己已成为好友
            SocketUtil.sendOneMessage(String.valueOf(dto.getInitUserId()), InteractMsgEnum.FRIEND_CONSENT.getMsg(),
                    InteractMsgEnum.FRIEND_CONSENT.getValue(), Long.valueOf(friendsApply.getId()));
        } else if (dto.getStatus().getValue() == 3) {
            msg.setContent(SecurityUtils.getNickName() + InteractMsgEnum.FRIEND_REFUSE.getMsg());
            msg.setType(InteractMsgEnum.FRIEND_REFUSE);
            //拒绝添加好友
            SocketUtil.sendOneMessage(String.valueOf(dto.getReceiveUserId()), SecurityUtils.getNickName() + InteractMsgEnum.FRIEND_REFUSE.getMsg(),
                    InteractMsgEnum.FRIEND_REFUSE.getValue(), Long.valueOf(friendsApply.getId()));
        }
        msg.setSourceId(Long.valueOf(friendsApply.getId()));
        consumerInteractMsgService.save(msg);

        return R.ok();
    }

    public R add(Long userId, Long receiveUserId) {
        if (ObjUtil.equal(userId, receiveUserId)) {
            return R.fail("无法添加自己");
        }
        SysUser receiveUser = sysUserMapper.selectUserById(receiveUserId);
        if (ObjUtil.isNull(receiveUser)) {
            return R.fail("好友未注册");
        }
        //检测是否已经成为好友
        LambdaQueryWrapper<ConsumerFriends> friendWp = new LambdaQueryWrapper<>();
        friendWp.eq(ConsumerFriends::getUserId, userId);
        friendWp.eq(ConsumerFriends::getFriendUserId, receiveUserId);
        ConsumerFriends one = consumerFriendsService.getOne(friendWp);
        if (ObjUtil.isNotNull(one)) {
            return R.fail("已添加为好友");
        }

        LambdaQueryWrapper<ConsumerFriendsApply> wq = new LambdaQueryWrapper<>();
        wq.eq(ConsumerFriendsApply::getInitUserId, userId);
        wq.eq(ConsumerFriendsApply::getStatus, ConsumerApplyStatusEnum.PENDING.getValue());
        wq.eq(ConsumerFriendsApply::getReceiveUserId, receiveUserId);
        Long count = consumerFriendsApplyMapper.selectCount(wq);
        if (ObjUtil.isNotNull(count) && count > 0) {
            update(new LambdaUpdateWrapper<ConsumerFriendsApply>()
                    .set(ConsumerFriendsApply::getCreateTime, LocalDateUtils.getCurrentLocalDateTime())
                    .eq(ConsumerFriendsApply::getInitUserId, userId)
                    .eq(ConsumerFriendsApply::getReceiveUserId, receiveUserId)
                    .eq(ConsumerFriendsApply::getStatus, ConsumerApplyStatusEnum.PENDING.getValue())
            );
            return R.ok();
        }

        ConsumerFriendsApply friendsApply = new ConsumerFriendsApply();
        friendsApply.setInitUserId(userId);
        friendsApply.setStatus(ConsumerApplyStatusEnum.PENDING.getValue());
        friendsApply.setReceiveUserId(receiveUserId);
        consumerFriendsApplyMapper.insert(friendsApply);

        //站内信V23.9.7 添加好友
        ConsumerInteractMsg msg = new ConsumerInteractMsg();
        msg.setUserId(receiveUserId);
        msg.setContent(SecurityUtils.getNickName() + InteractMsgEnum.FRIEND_ADD.getMsg());
        msg.setSourceId(Long.valueOf(friendsApply.getId()));
        msg.setType(InteractMsgEnum.FRIEND_ADD);
        consumerInteractMsgService.save(msg);
        //被申请方
        SocketUtil.sendOneMessage(String.valueOf(receiveUserId), SecurityUtils.getNickName() + InteractMsgEnum.FRIEND_ADD.getMsg(), InteractMsgEnum.FRIEND_ADD.getValue(), Long.valueOf(friendsApply.getId()));
        return R.ok();
    }
}
